<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    append-to-body
   width="35%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">

      <el-form-item label="房间号" prop="sa"  v-if="pricePId==null||pricePId==''" >
        <!-- <el-input v-model="dataForm.roomId" style="width: 200px;" placeholder="房间号"></el-input>
          <el-input v-model="dataForm.userid" style="width: 200px;" placeholder="用户"></el-input> -->
        <el-autocomplete v-model="dataForm.sa" style="width: 220px;" :fetch-suggestions="querySearchAsync" placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
      </el-form-item>
    <el-form-item label="留言人" prop="messagePerson" >
      <el-input v-model="dataForm.messagePerson" style="width: 220px;"  placeholder="留言人"></el-input>
    </el-form-item>
    <el-form-item label="自动提醒时间" prop="zdtxTime">
      <el-date-picker
        v-model="dataForm.zdtxTime"
        format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="自动提醒时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="有效时间" prop="yxTime">
      <el-date-picker
        v-model="dataForm.yxTime"
        format="yyyy-MM-dd HH:mm:ss"
        type="datetime"
        placeholder="有效时间">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="是否传达" prop="remack">
        <el-select v-model="dataForm.status" style="width: 220px;" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
    <!--  <el-input v-model="dataForm.remack" placeholder="备注"></el-input> -->
    </el-form-item>
    <el-form-item label="留言内容" prop="remack">
      <el-input type="textarea" v-model="dataForm.remack"  placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props:{
      foomFloorId:{
        type:String
      },
      pricePId:{
        type:String
      }
    },
    data () {
      return {
        options:[{
          value: '1',
          label: '是'
        }, {
          value: '2',
          label: '否'
        }],
        visible: false,
             restaurants: [],
        dataForm: {
          id: 0,
          fId: '',
          pId: '',
             sa:'',
             fjr:'',
          messagePerson: '',
          zdtxTime: '',
          yxTime: '',
          sellerId: '',
          status: '',
          remack: ''
        },
        dataRule: {

          messagePerson: [
            { required: true, message: '留言人不能为空', trigger: 'blur' }
          ],
          zdtxTime: [
            { required: true, message: '自动提醒时间不能为空', trigger: 'blur' }
          ],
          yxTime: [
            { required: true, message: '有效时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
       this.getList();
       if(this.pricePId!=null||this.pricePId!=''){
            this.dataForm.fjr=this.foomFloorId+"/"+this.pricePId
       }
    },
    methods: {
      handleSelect(item) {
         this.dataForm.fjr=item.id;
      },
      querySearchAsync(queryString, cb) {

        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;

        clearTimeout(this.timeout);
          cb(results);
      },
      createStateFilter(queryString) {
        return (state) => {
          return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      getList() {

        this.$http({
          url: this.$http.adornUrl('/hotel/trentalgoods/selectList'),
          method: 'post',
        }).then(({
          data
        }) => {

          this.restaurants = data.page;

        });

      },
      init (id) {
        this.getList();
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tcustomermessage/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {

              if (data && data.code === 0) {
                this.dataForm.fId = data.tCustomerMessage.fId
                this.dataForm.pId = data.tCustomerMessage.pId
                this.dataForm.messagePerson = data.tCustomerMessage.messagePerson
                this.dataForm.zdtxTime = data.tCustomerMessage.zdtxTime
                this.dataForm.yxTime = data.tCustomerMessage.yxTime
                this.dataForm.sellerId = data.tCustomerMessage.sellerId
                this.dataForm.status = data.tCustomerMessage.status
                this.dataForm.remack = data.tCustomerMessage.remack
                 this.dataForm.sa= data.tCustomerMessage.sa
                 this.dataForm.fjr=data.tCustomerMessage.fjr
              }
            })
          }
        })
      },
      getTime(time){
        var year =time.getFullYear();//获取完整的年份(4位,1970-????)
                        var month = time.getMonth() + 1;//获取当前月份(0-11,0代表1月)
                        var day = time.getDate();//获取当前日(1-31)
                        if (month < 10) {
                             month ="0" + month;
                         }
                        if (day < 10) {
                             day ="0" + day;
                         }
        var h= time.getHours();       // 获取当前小时数(0-23)
         var s=time.getMinutes();     // 获取当前分钟数(0-59)
         var a=time.getSeconds();
          if (h < 10) {
                              h ="0" + h;
                          }
                         if (s < 10) {
                              s ="0" + s;
                          }
          if (a < 10) {
                              a ="0" + a;
                          }
                       
         var dateString = year +"-" + month + "-" + day  +"  "+h+":"+s+":"+a;

         return dateString;
      },
      // 表单提交
      dataFormSubmit () {
        if(this.dataForm.yxTime.constructor==Date){
          this.dataForm.yxTime=this.getTime(this.dataForm.yxTime)
        }
        if(this.dataForm.zdtxTime.constructor==Date){
          this.dataForm.zdtxTime=this.getTime(this.dataForm.zdtxTime)
        }
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/tcustomermessage/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                 'fjr':this.dataForm.fjr,
                'messagePerson': this.dataForm.messagePerson,
                'zdtxTime': this.dataForm.zdtxTime,
                'yxTime': this.dataForm.yxTime,
                'sellerId': this.dataForm.sellerId,
                'status': this.dataForm.status,
                'remack': this.dataForm.remack
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
